<template>
  <div>
    <el-dialog
      :title="menuForm.show == true ? '编辑' : '新增'"
      :visible.sync="is.show"
      @close="exit"
    >
      <el-form :model="menuForm" :rules="rules" ref="menuForm">
        <el-form-item
          prop="title"
          label="菜单名称"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="menuForm.title"
            clearable
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <el-select
            v-model="menuForm.pid"
            @change="changePid"
            placeholder="请选择上级菜单"
          >
            <el-option label="顶级菜单" :value="0"></el-option>

            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="menuForm.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menuForm.type" :label="2">菜单</el-radio>
        </el-form-item>

        <el-form-item
          v-if="menuForm.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <!-- <el-input
            v-model="menuForm.icon"
            clearable
            autocomplete="off"
          ></el-input> -->
          <el-select
            v-model="menuForm.icon"
            placeholder="请选择菜单图标"
          >
            <el-option v-for="item in iconArr" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <!-- <el-input
            v-model="menuForm.url"
            clearable
            autocomplete="off"
          ></el-input> -->
          <el-select
            v-model="menuForm.url"
            placeholder="请选择菜单地址"
          >
            <el-option v-for="item in urlArr" :key="item.path" :label="item.path" :value="item.path">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="exit">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入封装好的menu 添加接口
import { addOreditMenu } from "../../request/api";
//引入菜单地址 
import {indexChild} from '../../router';
export default {
  data() {
    return {
      //表单数据
      menuForm: {
        title: "",
        type: 1,
        icon: "",
        url: "",
        pid: 0,
        status: 1,
      },
      formLabelWidth: "120px",
      //表单验证规则
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 6, message: "请输入2-6位字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "blur" }],
      },
      //图标数组
      iconArr: [
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-more",
        "el-icon-s-goods",
        "el-icon-warning",
        "el-icon-info",
        "el-icon-star-on",
        "el-icon-message-solid",
        "el-icon-s-home",
        "el-icon-location",
      ],
      //菜单地址
      urlArr:indexChild
    };
  },

  //计算属性 用来使用辅助性函数  mapState
  computed: {
    ...mapState("menu", ["menuList", "is"]),
  },

  //方法
  methods: {
    //辅助性函数 mapActions
    ...mapActions("menu", ["getMenuList"]),

    //点击取消事件  直接触发vuex的关闭Dialog事件
    exit() {
      this.$store.commit("menu/changeDialog", false);
      //重置表单以及验证规则
      this.$refs.menuForm.resetFields();
      this.menuForm = {
        title: "",
        type: "1",
        icon: "",
        url: "",
        pid: 0,
        status: 1,
      };
    },

    //点击确认按钮 提交表单 清空输入
    enter() {
      this.$refs.menuForm.validate((res) => {
        //这是表单是否通过验证
        if (res) {
          // console.log(this.menuForm, "111111111111");
          //把添加 修改放在一起  通过show  来判断具体是哪一个
          let url = this.menuForm.show ? "menuedit" : "menuadd";
          //调用menu添加接口
          addOreditMenu(url, this.menuForm).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //点击确认 关闭弹窗 清空输入的内容
              this.exit();
              //重新获取添加后的列表
              this.getMenuList();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },

    //子组件定义方法 用来接收穿过来的 编辑资料  用来回显数据
    dataForm(obj) {
      // console.log(obj,'子组件接收数据');
      //数据回显
      this.menuForm = { ...obj };
    },

    //选择下拉列表 切换菜单类型
    changePid() {
      this.menuForm.type = this.menuForm.pid == 0 ? 1 : 2;
    },
  },
};
</script>


<style scoped>
</style>